﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace WarPigs
{
    class PlayerManager
    {
        int numPlayers;
        Map map;
        WarPigs game;
        LocalPlayerData localPlayerData;
        public Player[] players;

        public PlayerManager(int NumPlayers, Map Map, LocalPlayerData LocalPlayerData, WarPigs Game)
        {
            numPlayers = NumPlayers;
            map = Map;
            game = Game;
            localPlayerData = LocalPlayerData;
            players = new Player[numPlayers];
            //sets player0 to localPlayerData
            //if(localPlayerData != null)
            CreateLocalPlayer();
            //Generates remaining players
            InitializePlayers();
        }

        private void CreateLocalPlayer()
        {
            Player player = new Player(game.localPlayerData.data.pmcName, game.localPlayerData.data.pmcColorC, 1000, 0);
            players[0] = player;
            player.hqCoordinates = GetHQCoordinates();
            Build("Headquarters", "player0", player.hqCoordinates);
        }

        private void InitializePlayers()
        {
            for (int x = 1; x < numPlayers; x++)
            {
                CreatePlayer(x);
            }
        }

        private void CreatePlayer(int x)
        {
            string playername = "player" + x;
            Player player = new Player(GetName(x), GetColor(x), game.difficulty.money, 0);
            players[x] = player;            
            Build("Headquarters", playername, GetHQCoordinates());
        }

        private Color GetColor(int x)
        {
            return Color.SteelBlue;
        }

        private string GetName(int x)
        {
            return "pmcName";
        }

        private Vector2 GetHQCoordinates()
        {
            Vector2 coordinates = new Vector2();
            Random random = new Random();
            int ranX = random.Next(10, map.terrainarray.GetLength(0) - 10);
            int ranY = random.Next(10, map.terrainarray.GetLength(1) - 10);
            if (map.terrainarray != null)
            {
                while (0 < 1)
                {
                    if (map.ownershiparray[ranX, ranY] == 0 && map.ownershiparray[ranX + 1, ranY] == 0 && map.ownershiparray[ranX - 1, ranY] == 0 && map.ownershiparray[ranX, ranY + 1] == 0 &&
                    map.ownershiparray[ranX, ranY - 1] == 0 && map.ownershiparray[ranX + 1, ranY + 1] == 0 && map.ownershiparray[ranX + 1, ranY - 1] == 0)
                    {
                        if (map.terrainarray[ranX, ranY] == 1 || map.terrainarray[ranX, ranY] == 5 || map.terrainarray[ranX, ranY] == 6 || map.terrainarray[ranX, ranY] == 7 && map.ownershiparray[ranX, ranY] == 0)
                        {
                            if (ranY % 2 != 0)
                            {
                                coordinates = new Vector2(ranX, ranY);
                                break;
                            }
                            else
                            {
                                ranX = random.Next(10, map.terrainarray.GetLength(0) - 10);
                                ranY = random.Next(10, map.terrainarray.GetLength(1) - 10);
                                continue;
                            }
                        }
                        else
                        {
                            ranX = random.Next(10, map.terrainarray.GetLength(0) - 10);
                            ranY = random.Next(10, map.terrainarray.GetLength(1) - 10);
                            continue;
                        }
                    }
                    else
                    {
                        ranX = random.Next(10, map.terrainarray.GetLength(0) - 10);
                        ranY = random.Next(10, map.terrainarray.GetLength(1) - 10);
                        continue;
                    }
                }
            }
            
            return coordinates;
        }

        public void Build(string Building, string faction, Vector2 Coordinate)
        {
            if (Coordinate.X > 0)
            {
                switch (Building)
                {
                    case "Headquarters":
                        //Sets the building
                        map.buildingarray[(int)Coordinate.X, (int)Coordinate.Y] = 1;
                        //Lays the control zone
                        UpdateOwnership((int)Coordinate.X, (int)Coordinate.Y, faction);
                        break;
                }
            }
        }

        public void UpdateOwnership(int x, int y, string faction)
        {
            switch (faction)
            {
                case "player0":
                    //Ownership
                    map.ownershiparray[x, y] = 6;
                    map.ownershiparray[x + 1, y] = 6;
                    map.ownershiparray[x - 1, y] = 6;
                    map.ownershiparray[x, y + 1] = 6;
                    map.ownershiparray[x, y - 1] = 6;
                    map.ownershiparray[x + 1, y + 1] = 6;
                    map.ownershiparray[x + 1, y - 1] = 6;

                    //Initial Fog of War
                    map.fogofwar[x, y] = 2;
                    map.fogofwar[x + 1, y] = 2;
                    map.fogofwar[x - 1, y] = 2;
                    map.fogofwar[x, y + 1] = 2;
                    map.fogofwar[x, y - 1] = 2;
                    map.fogofwar[x + 1, y + 1] = 2;
                    map.fogofwar[x + 1, y - 1] = 2;

                    break;
                case "player1":
                    map.ownershiparray[x, y] = 7;
                    map.ownershiparray[x + 1, y] = 7;
                    map.ownershiparray[x - 1, y] = 7;
                    map.ownershiparray[x, y + 1] = 7;
                    map.ownershiparray[x, y - 1] = 7;
                    map.ownershiparray[x + 1, y + 1] = 7;
                    map.ownershiparray[x + 1, y - 1] = 7;

                    //Initial Fog of War
                    map.fogofwar[x, y] = 2;
                    map.fogofwar[x + 1, y] = 2;
                    map.fogofwar[x - 1, y] = 2;
                    map.fogofwar[x, y + 1] = 2;
                    map.fogofwar[x, y - 1] = 2;
                    map.fogofwar[x + 1, y + 1] = 2;
                    map.fogofwar[x + 1, y - 1] = 2;
                    break;
                case "player2":
                    map.ownershiparray[x, y] = 8;
                    map.ownershiparray[x + 1, y] = 8;
                    map.ownershiparray[x - 1, y] = 8;
                    map.ownershiparray[x, y + 1] = 8;
                    map.ownershiparray[x, y - 1] = 8;
                    map.ownershiparray[x + 1, y + 1] = 8;
                    map.ownershiparray[x + 1, y - 1] = 8;

                    //Initial Fog of War
                    map.fogofwar[x, y] = 2;
                    map.fogofwar[x + 1, y] = 2;
                    map.fogofwar[x - 1, y] = 2;
                    map.fogofwar[x, y + 1] = 2;
                    map.fogofwar[x, y - 1] = 2;
                    map.fogofwar[x + 1, y + 1] = 2;
                    map.fogofwar[x + 1, y - 1] = 2;
                    break;

                case "player4":
                    map.ownershiparray[x, y] = 9;
                    map.ownershiparray[x + 1, y] = 9;
                    map.ownershiparray[x - 1, y] = 9;
                    map.ownershiparray[x, y + 1] = 9;
                    map.ownershiparray[x, y - 1] = 9;
                    map.ownershiparray[x + 1, y + 1] = 9;
                    map.ownershiparray[x + 1, y - 1] = 9;

                    //Initial Fog of War
                    map.fogofwar[x, y] = 2;
                    map.fogofwar[x + 1, y] = 2;
                    map.fogofwar[x - 1, y] = 2;
                    map.fogofwar[x, y + 1] = 2;
                    map.fogofwar[x, y - 1] = 2;
                    map.fogofwar[x + 1, y + 1] = 2;
                    map.fogofwar[x + 1, y - 1] = 2;
                    break;

                case "player5":
                    map.ownershiparray[x, y] = 10;
                    map.ownershiparray[x + 1, y] = 10;
                    map.ownershiparray[x - 1, y] = 10;
                    map.ownershiparray[x, y + 1] = 10;
                    map.ownershiparray[x, y - 1] = 10;
                    map.ownershiparray[x + 1, y + 1] = 10;
                    map.ownershiparray[x + 1, y - 1] = 10;

                    //Initial Fog of War
                    map.fogofwar[x, y] = 2;
                    map.fogofwar[x + 1, y] = 2;
                    map.fogofwar[x - 1, y] = 2;
                    map.fogofwar[x, y + 1] = 2;
                    map.fogofwar[x, y - 1] = 2;
                    map.fogofwar[x + 1, y + 1] = 2;
                    map.fogofwar[x + 1, y - 1] = 2;
                    break;
            }
        }
    }
}
